# Implement the rescaled change time method on single-day data
# For AAPL 2019-01-02, WITHSIZE, SUPPORT 4, DELTA 0.5

library(data.table)
library(tidyr)
library(xts)
library(dplyr)
library(pbmcapply)
load("~/Desktop/orderbook/Research/R/gof/AAPL_2019-01-02.RData")

support_max = 20
delta = 0.5
config = "nosize"
#config = ""
if(config == "nosize"){
  event_size = ""
}else{
  event_size = "eventsize_"
}

#model_config = "_state_hawkes_time_customizedtwo_LASSO_0005"
model_config = "_hawkes_LASSO_0005"
# Load Hawkes estimation data
hawkes = readRDS(paste0("~/Desktop/orderbook/Research/R/gof/images/hawkes_markov_pref_price_", event_size,
                        data_file_name, paste0("_support",toString(support_max),"delta", toString(delta),model_config,".rds") ))


delta = hawkes$delta
support_max = hawkes$support_max
support_seq = seq(0,support_max,delta)

book =  book[modifying_queue %in% -3:3]

book = book[, c("time","queue_and_event")]


# Selected event for goodness-of-fit testing is "+2(+)"
event_names = colnames(event)[-1]





# get change time
change_time = c()
change_time = pbmclapply(1:dim(book)[1], function(i){
  c(change_time, seq(book$time[i], book$time[i]+support_max, delta  ))
},mc.cores = 8)
change_time = unlist(change_time)
change_time = sort(unique(change_time))
change_time = data.frame("time"= change_time)



rm(price, state,state_list, book_lambda, book_event, book_msg, book_order,event, ref_price_nondup, 
   average_event_size,estimate_hawkes, plot_hawkes, get_theta_cls, get_hawkes_kernel_bigdt,
   get_bigdt_list, get_theta_cls_bigdt_fromfile, plot_hawkes_kernel, organize_kernel_output, 
   get_theta_cls_bigdt,box_kernel_var,event_size_by_ticker)
gc()

change_time = change_time$time

#event_index = readRDS(paste0("~/Desktop/orderbook/Research/R/gof/images/event_index_",data_file_name,"_support",toString(support_max),"delta", toString(delta),"_", event_size,".rds"  ))
event_index = readRDS(paste0("~/Desktop/orderbook/Research/R/gof/images/event_index_",data_file_name,"_support",toString(support_max),"delta", toString(delta),"_", "eventsize",".rds"  ))


for (k in 1:1){
  selected_event = event_names[k]
  lambda_list = readRDS(file = paste0("~/Desktop/orderbook/Research/R/gof/images/lambda_",event_size,data_file_name,"_support",toString(support_max),"delta",toString(delta),model_config,"_",selected_event,".rds"   ) )
  selected_event = event_names[k]
  selected_index = event_index[which(book$queue_and_event == selected_event)]
  integral_diff = pbmclapply(2:length(selected_index), function(i){
    #    print(i)
    curr_index = selected_index[[i]][1]
    previous_index = selected_index[[i-1]][1]
    sum(lambda_list[previous_index: (curr_index-1)] * diff(change_time[previous_index:curr_index]))
  }, mc.cores = 8)
  empirical = unlist(integral_diff)
  print (mean(empirical))
  print (var(empirical))
  saveRDS(empirical, paste0("~/Desktop/orderbook/Research/R/gof/images/empirical_",event_size,data_file_name,"_support",toString(support_max), "delta", toString(delta),model_config,"_",selected_event,".rds" ))
  rm(lambda_list,integral_diff,empirical)
  
}


